package com.xue.utils;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;
import java.io.*;
import java.sql.*;
import java.util.*;

/**
 * @author xue
 * @ClassName: JdbcUtils
 * @Date: Created in 22:33 2021/10/30
 * @Description:
 */
public class JdbcUtils {

    public static Connection getConnection() {
        InputStream is = null;
        Connection connection = null;
        try {
//            is = new FileInputStream("src/test/resources/jdbc.properties");
            is = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties pro = new Properties();
            pro.load(is);
            String username = pro.getProperty("username");
            String password = pro.getProperty("password");
            String url = pro.getProperty("url");
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return connection;
    }

    public static void closeResource(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Object queryValue(String sql){
        Connection connection = getConnection();
        QueryRunner queryRunner =new QueryRunner();
        Object result = "";
        try {
            result = queryRunner.query(connection, sql, new ScalarHandler<>());
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeResource(connection);
        }
        return result;
    }

    public static Object querySingleData(String sql){
        Connection connection = getConnection();
        QueryRunner queryRunner =new QueryRunner();
        Object result = "";
        try {
            result = queryRunner.query(connection, sql, new MapHandler());
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeResource(connection);
        }
        return result;
    }


    public static List<Map<String, Object>> queryListData(String sql){
        Connection connection = getConnection();
        QueryRunner queryRunner =new QueryRunner();
        List<Map<String, Object>> result = null;
        try {
            result = queryRunner.query(connection, sql, new MapListHandler());
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeResource(connection);
        }
        return result;
    }

    public static int update(String sql){
        Connection connection = getConnection();
        QueryRunner queryRunner =new QueryRunner();
        int result=0;
        try {
            result = queryRunner.update(connection, sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeResource(connection);
        }
        return result;
    }

}
